package com.zq.im.retrofit;


import com.zq.im.network.Urls;
import com.zq.im.utils.SharePreferenceUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by to-explore-future on 2020/3/30
 */
public class RetrofitManager {
    private static Retrofit mRetrofit;

    public static Retrofit getRetrofit() {
        if (mRetrofit == null) {
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(Urls.baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(genericClient())
                    .build();
        }
        return mRetrofit;
    }



    public static OkHttpClient genericClient() {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Interceptor.Chain chain) throws IOException {
                        Request request = chain.request()
                                .newBuilder()
                                .addHeader("token", SharePreferenceUtils.getString("token"))
                                .build();
                        return chain.proceed(request);
                    }
                }).connectTimeout(60, TimeUnit.SECONDS)  // 设置连接超时时间
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
//                .proxy(Proxy.NO_PROXY)  //禁止抓包
                .build();
        return httpClient;
    }
}
